using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Module.CMS.Library;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Distributions;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.Contents;

namespace NCS.Module.CMS.Web.Sport
{
    public partial class ContentList : CMSWebControl
    {
        public string lblPage = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (curZone == null) return;
            lblZoneName.Text = curZone.Zone_Name;
            if (curZone.Zone_Type == 1)
            {
                pnlContentList.Visible = false;
                pnlSpecialContent.Visible = true;
                DataTable dtSpecialContent = DistributionController.GetDistributedContents(curZone.Zone_ID, 1, (int)CMSDistributionRank.Special);
                DataRow specialRow = null;
                if (dtSpecialContent != null && dtSpecialContent.Rows.Count > 0) specialRow = dtSpecialContent.Rows[0];
                if (specialRow != null)
                {
                    if (specialRow["Content_Avatar"].ToString() != string.Empty)
                    {
                        imgSpecialAvatar.ImageUrl = specialRow["Content_Avatar"].ToString();
                    }
                    else
                    {
                        imgSpecialAvatar.Visible = false;
                        divSpecialAvatar.Visible = false;
                    }
                    lblSpecialHeadline.Text = specialRow["Content_HeadLine"].ToString();
                    litSpecialTeaser.Text = specialRow["Content_Teaser"].ToString();
                    litSpecialContentBody.Text = specialRow["Content_Body"].ToString();
                }

            }
            else
            {
                int totalRecord;
                if (Request.QueryString["page"] != null)
                {
                    txtHiddenValue.Text = ConvertUtility.ToString((ConvertUtility.ToInt32(Request.QueryString["page"]) - 1));
                }
                else
                {
                    txtHiddenValue.Text = "0";
                }
                int pagesize = 10;
                DataTable dtContents = DistributionController.GetNewContentByZoneIDAndRank(curZone.Zone_ID, false, pagesize, ConvertUtility.ToInt32(txtHiddenValue.Text), out totalRecord);
                rptContentList.DataSource = dtContents;
                rptContentList.ItemDataBound +=new RepeaterItemEventHandler(rptContentList_ItemDataBound);
                rptContentList.DataBind();

                #region paging
                
                double pagenum = totalRecord % pagesize;
                if( pagenum == 0 )
                {
                    for (int i = 1; i < totalRecord / pagesize + 1; i++)
                    {
                        if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                        {
                            if (i == 1)
                            {
                                lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                            }
                            else
                            {
                                lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                            }
                        }
                        else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                }
                else if (totalRecord % pagesize > 0)
                {
                    for (int i = 1; i < totalRecord / pagesize + 2; i++)
                    {
                        if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                        {
                            if (i == 1)
                            {
                                lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                            }
                            else
                            {
                                lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                            }
                        }
                        else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                }
                #endregion
            }
        }

        void rptContentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                Label lbalias = (Label)e.Item.FindControl("lbalias");
                lbalias.Text = (ConvertUtility.ToString(currow["Content_Alias"]) == string.Empty ? "" : (currow["Content_Alias"].ToString() + "  ")) + ConvertUtility.FormatTimeVn(ConvertUtility.ToDateTime(currow["Distribution_CreateDate"]));
            }
        }       

    }
}